<template>
  <a-modal :open="open" width="640px" title="撤销费用" :ok-button-props="{ disabled: !isCompleted }" ok-text="确认撤销" centered @ok="onOk" @cancel="onCancel">
    <div class="pl-12 pr-2 pt-1 mb-2 pb-1 border-dashed border-0 border-b-2 bg-black-1">
      <div class="flex text-center justify-between">
        <div class="flex text-center">
          <MedicineInfo :value="{ mc: formState?.advName, gg: formState?.spc }">
            <template #suffix>
              <div name="#suffix" class="text-gray-400 ml-4 text-xs">{{ formState?.minimumCount }} {{ formState?.minimumUnit }}</div>
            </template>
          </MedicineInfo>
        </div>
        <div class="text-red-600 font-bold">￥{{ formatAmount(formState?.settlementAmount) }}</div>
      </div>
    </div>
    <a-form :model="formState" class="form-style-flex mt-3" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="批准人" class="w-1/2" v-bind="validateInfos.empId">
        <a-input v-model:value="formState.empId" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="密码" class="w-1/2" v-bind="validateInfos.pwd">
        <a-input v-model:value="formState.pwd" type="password" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import { formatAmount } from '@/utils/format.js'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  },
  selectedItem: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  advName: '',
  unit: '',
  amount: '',
  empId: '',
  pwd: ''
})
const rulesState = ref({
  empId: [{ required: true, message: '请输入批准人' }],
  pwd: [{ required: true, message: '请输入批准人密码' }]
})
const { isCompleted, validate, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  await validate()
  const res = await post('/inp/inpComprehensive/doCancelExpenses', {
    ...formState.value,
    status: 1,
    ids: [formState.value.id],
    inplsh: props.selectedItem.inplsh
  })
  if (res.code !== 0) return message.error(res.msg || '操作失败')
  message.success(res.msg || '保存成功')
  emit('success')
  onCancel()
}
watch(
  () => props.open,
  (val) => {
    if (val && props.rowData?.id) {
      formState.value = { ...props.rowData }
    }
  },
  {
    immediate: true
  }
)

const onCancel = () => {
  emit('update:open', false)
}
</script>
<style lang="less">
.bg-black-1 {
  background: rgba(0, 0, 0, 0.1);
}
</style>
